package com.hsteach.web.controller.visit;

import com.hsteach.common.annotation.Log;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.domain.model.Eclass;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.enums.BusinessType;
import com.hsteach.common.enums.OperatorType;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.dao.domain.visit.VisitFamilyInfo;
import com.hsteach.dao.domain.visit.VisitFamilyPerson;
import com.hsteach.dao.domain.visit.VisitRecord;
import com.hsteach.enums.VisitDataTypeEnum;
import com.hsteach.service.third.XgkService;
import com.hsteach.service.visit.VisitFamilyInfoService;
import com.hsteach.service.visit.VisitFamilyPersonService;
import com.hsteach.service.visit.VisitInterestTagService;
import com.hsteach.service.visit.VisitRecordService;
import com.hsteach.util.SeasonUtil;
import com.hsteach.web.controller.visit.dto.*;
import com.hsteach.web.controller.visit.vo.ListStudentVo;
import com.hsteach.web.controller.visit.vo.StudentRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 学生家庭信息Controller
 *
 * @author fanxquer
 * @date 2023-07-21
 */
@Api(value = "家访-学生列表", tags = "家访-学生列表")
@RestController
@RequestMapping("/visit/student")
public class VisitFamilyInfoController extends BaseController
{
    @Autowired
    private VisitFamilyInfoService visitFamilyInfoService;

    @Autowired
    private VisitInterestTagService visitInterestTagService;

    @Autowired
    private VisitFamilyPersonService visitFamilyPersonService;

    @Autowired
    private VisitRecordService visitRecordService;

    @Autowired
    private XgkService xgkService;

    /**
     * 查询学生列表
     */
    @ApiOperation("查询学生列表")
    @PostMapping("/list")
    public TableDataInfo<ListStudentVo> list(@RequestBody ListStudentDto dto)
    {
//        List<ListStudentVo> list = ;
        return visitFamilyInfoService.listStudent(dto);
    }

    /**
     * 查询执教班级列表
     */
    @ApiOperation("查询执教班级列表")
    @GetMapping("/classesList")
    public R<List<Eclass>> classesList()
    {
//        List<GetClazzWithStudentsRet> clazzWithStudents = xgkService.getClazzWithStudents(SecurityUtils.getAuthInfo().getPersonId());
//        List<Eclass> ret = clazzWithStudents.stream().map(e -> {
//            Eclass eclass = new Eclass();
//            eclass.setEclassName(e.getClassName());
//            eclass.setEclassId(e.getClassId());
//            return eclass;
//        }).collect(Collectors.toList());
        List<Eclass> list = SecurityUtils.getAuthInfo().getEclasses();
        return R.ok(list);
    }

    /**
     * 查询学生基本信息
     */
    @ApiOperation("查询学生基本信息")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") String personId)
    {
        return visitFamilyInfoService.getInfo(personId);
    }

    /**
     * 查询重点关注项
     */
    @ApiOperation("查询重点关注项")
    @GetMapping(value = "/tags/{personId}")
    public AjaxResult getTags(@PathVariable("personId") String personId, @RequestParam(value = "recordId", required = false) Long recordId)
    {
//        VisitInterestTag query = new VisitInterestTag();
//        query.setPersonId(personId);
//        if (recordId != null) {
//            query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
//            query.setVisitRecordId(recordId);
//        }
//        query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
//        List<VisitInterestTag> visitInterestTags = visitInterestTagService.selectVisitInterestTagList(query);
//        visitInterestTags.stream().forEach(e -> {
//            e.setTagStr(InterestEnum.getNameByCode(e.getTag()));
//        });
        return visitFamilyInfoService.getTagInfo(personId, recordId);
    }

    /**
     * 修改重点关注项
     */
    @ApiOperation("修改重点关注项")
    @Log(title = "修改重点关注项", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @PutMapping(value = "/tags")
    public AjaxResult editTags(@RequestBody UpdateTagsDto dto)
    {
        return toAjax(visitInterestTagService.updateTags(dto));
    }

    /**
     * 查询家庭信息
     */
    @ApiOperation("查询家庭信息，返回code为-2代表没有家庭信息")
    @GetMapping(value = "/family/{personId}")
    public AjaxResult getFamilyInfo(@PathVariable("personId") String personId, @RequestParam(value = "recordId", required = false) Long recordId)
    {
        return visitFamilyInfoService.getFamilyInfo(personId, recordId);
    }

    /**
     * 新增家庭信息
     */
    @ApiOperation("新增家庭信息")
    @Log(title = "新增家庭信息", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @PostMapping(value = "/family")
    public AjaxResult addFamilyInfo(@RequestBody @Validated AddFamilyInfoDto dto)
    {

        VisitFamilyInfo init = new VisitFamilyInfo();
        init.setPersonId(dto.getPersonId());
        init.setDescribeText(dto.getDescribeText());
        init.setStructure(dto.getStructure());
        if (dto.getRecordId() == null) {
            init.setDataType(VisitDataTypeEnum.STUDENT.getCode());
        } else {
            init.setDataType(VisitDataTypeEnum.RECORD.getCode());
            init.setRecordId(dto.getRecordId());
        }
        init.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
        init.setCreateByName(SecurityUtils.getAuthInfo().getName());
//        visitFamilyInfoService.insertVisitFamilyInfo(init);
        return toAjax(visitFamilyInfoService.insertVisitFamilyInfo(init));
    }

    /**
     * 修改学生家庭信息
     */
    @ApiOperation("修改家庭信息")
    @Log(title = "修改家庭信息", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @PutMapping(value = "/family")
    public AjaxResult edit(@RequestBody UpdateFamilyInfoDto dto)
    {
        return toAjax(visitFamilyInfoService.updateVisitFamilyInfo(dto));
    }

    /**
     * 查询家庭成员信息
     */
    @ApiOperation("查询家庭成员信息")
    @GetMapping(value = "/family/person/{personId}")
    public AjaxResult getFamilyPersonInfo(@PathVariable("personId") String personId, @RequestParam(value = "recordId", required = false) Long recordId)
    {
        VisitFamilyPerson query = new VisitFamilyPerson();
        query.setPersonId(personId);
        if (recordId != null) {
            query.setDataType(VisitDataTypeEnum.RECORD.getCode());
            query.setRecordId(recordId);
        } else {
            query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
        }
        List<VisitFamilyPerson> visitFamilyPeople = visitFamilyPersonService.selectVisitFamilyPersonList(query);
        return success(visitFamilyPeople);
//        return visitFamilyPersonService.getFamilyPersonInfo(personId, recordId);
//        return success(visitFamilyPersonService.getFamilyPersonInfo(personId, recordId));
    }


    /**
     * 新增学生家庭成员信息
     */
    @ApiOperation("新增学生家庭成员信息")
    @Log(title = "新增学生家庭成员信息", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @PostMapping(value = "/family/person")
    public AjaxResult add(@RequestBody AddFamilyPersonDto dto)
    {
        VisitFamilyPerson visitFamilyPerson = new VisitFamilyPerson();
        BeanUtils.copyProperties(dto, visitFamilyPerson);
//        if (dto.getRecordId() != null) {
//            visitFamilyPerson.setDataType(VisitDataTypeEnum.RECORD.getCode());
//        } else {
//            visitFamilyPerson.setDataType(VisitDataTypeEnum.STUDENT.getCode());
//        }
        return toAjax(visitFamilyPersonService.insertVisitFamilyPerson(visitFamilyPerson));
    }

    /**
     * 删除学生家庭成员信息
     */
    @ApiOperation("删除学生家庭成员信息")
    @Log(title = "删除学生家庭成员信息", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @DeleteMapping(value = "/family/person/{id}")
    public AjaxResult remove(@PathVariable Long id, @RequestParam(value = "recordId", required = false) Long recordId)
    {
        return toAjax(visitFamilyPersonService.deleteVisitFamilyPerson(id, recordId));
    }

    /**
     * 查询家访记录
     */
    @ApiOperation("查询家访记录")
    @GetMapping(value = "/record/{personId}")
    public R<List<StudentRecordVo>> getRecord(@PathVariable("personId") String personId)
    {
        VisitRecord query = new VisitRecord();
        query.setPersonId(personId);
        query.setClassIds(xgkService.mainClassId(SecurityUtils.getAuthInfo().getPersonId()));
        query.setCurrPersonId(SecurityUtils.getAuthInfo().getPersonId());
        query.setOrderBy("visit_date");
        List<VisitRecord> visitRecords = visitRecordService.selectVisitRecordList(query);
        List<StudentRecordVo> ret = Lists.newArrayList();
        for (int i = 0;i < visitRecords.size(); i++) {
            VisitRecord record = visitRecords.get(i);
            StudentRecordVo build = StudentRecordVo.builder()
                    .name(record.getRecordName())
                    .visitDate(record.getVisitDate())
                    .id(record.getId())
                    .createTime(record.getCreateTime())
                    .seasonStr(SeasonUtil.getSeasonStr(record.getCreateTime()))
                    .visitTime(record.getVisitDate())
                    .build();
            ret.add(build);
        }
        return R.ok(ret);
    }
}
